package com.zch.market.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zch.market.dao.ProviderMapper;
import com.zch.market.entity.Provider;
import com.zch.market.service.ProviderService;
@Service("ps")
public class ProviderServiceImpl implements ProviderService {
	@Autowired
	private ProviderMapper dao;
	//每页的数据个数
	private final int PAGE_SIZE=4;
	@Override
	public List<Provider> getProviderByPage(String name, String desc, int pageIndex) {
		//使用集合来存放可变参数
		Map<String, Object> map = new HashMap<>();
		map.put("name", name);
		map.put("desc", desc);
		//获得当前页第一条数据在查询的数据中的位置
		int firstRow = (pageIndex - 1)*PAGE_SIZE+1;
		//获得当前页最后一条数据在查询的数据中的位置
		int lastRow = pageIndex*PAGE_SIZE;
		map.put("firstRow", firstRow);
		map.put("lastRow", lastRow);
		List<Provider> list = dao.getProviderByPage(map);
		return list;
	}

	@Override
	public int getTotalPage(String name, String desc) {
		//使用集合来存放可变参数
		Map<String, Object> map = new HashMap<>();
		map.put("name", name);
		map.put("desc", desc);
		//获得查询到的总数据个数
		int counts = dao.getTotalCount(map);
		//获得页数
		int pageCounts = counts%PAGE_SIZE==0?counts/PAGE_SIZE:counts/PAGE_SIZE+1;
		return pageCounts;
	}
	@Override
	public int getTotalCount(String name, String desc) {
		//使用集合来存放可变参数
		Map<String, Object> map = new HashMap<>();
		map.put("name", name);
		map.put("desc", desc);
		//获得查询到的总数据个数
		int counts = dao.getTotalCount(map);
		return counts;
	}
	
	

	@Override
	public Provider getProviderById(int id) {
		
		return dao.getProviderById(id);
	}

	@Override
	public boolean addProvider(Provider provider) {
		
		return dao.addProvider(provider);
	}

	@Override
	public boolean deleteProvider(int id) {
		
		return dao.deleteProvider(id);
	}

	@Override
	public boolean updateProvider(Provider provider) {
		
		return dao.updateProvider(provider);
	}

}
